#!/bin/bash

# Ensure the script is being run by root
if [ $(id -u) -ne 0 ];then
	echo "$0 must be run as root"
	exit 1
fi

# Source the lib file
. lib.sh

# print out the usage of the script
function usage {
	echo "==========================================="
	echo "Usage:$#"
	echo "run.sh [options]"
	echo "-a | --all  -  harden all vulnerabilities"
	echo "-o | --system  -  harden system items"
	echo "-s | --services  -  harden services items"
	echo "-l | --log  -  log all actions"
	echo "-? | -h | --help  -  show this message"
	echo "==========================================="
}

# Need to account of the "all" case not going to the next script
function runScript {
	while [[ -z $ANSWER || $ANSWER = a ]]; do
		if [[ ${ANSWER} != a ]]; then
			echo $QUESTION 
			echo $ANSWER_KEY
			read ANSWER #Read user input and set to ANSWER
			#ANSWER=${ANSWER,,*} #Translate all capitals to lowercase
			ANSWER=$(echo $ANSWER | tr [:upper:] [:lower:])
		fi
		#echo Script is $SCRIPT
		#echo Question is $QUESTION
		#echo Solution is $SOLUTION
		#echo Answer is $ANSWER
		case $ANSWER in
			y | yes)	eval $SOLUTION 1>&2 #using eval allows for multiple commands to be stored in SOLUTION
						break;;
			n | no)		echo -e "Skipping\n"
						break;;
			a | all)	eval $SOLUTION 1>&2
						echo -e "Executing all fixes\n"
						break;;
			d | description) echo -e "$DESCRIPTION\n"
						ANSWER=
						;;
			q | quit)	echo -e "Exiting the program\n"
						exit;;
			*)			echo -e "input unrecognized\n"
						ANSWER=
						;;
		esac

	done

	ANSWER=${ANSWER:0:1}
}

function log {
	while [[ $# > 0 ]]; do
		case $1 in
			newline) echo -e "\n" >> $LOG_FILE
					;;
			time) echo -e "`date +%F' '%H:%M:%S`" >> $LOG_FILE
					;;
			script) echo -e "Script is $SCRIPT" >> $LOG_FILE
					;;
			question) echo -e "Question is $QUESTION" >> $LOG_FILE
					;;
			answer) echo -e "Answer is ${ANSWER/a/y}" >> $LOG_FILE
					;;
			precheck) echo -e "$PRECHECK" >> $LOG_FILE
					;;
			solution) echo -e "Solution was:\n$SOLUTION" >> $LOG_FILE
		esac
		shift
	done
}

# Initialize variables needed for this script
ANSWER_KEY=" (y[es] | n[o] | a[ll] | q[uit] | d[escription]) " # Statement that prints after each question

# while loop to handle any and all options 
while [[ "$#" > 0  ]]; do
	case $@ in
		-a | --all)SCRIPT_DIRS_TO_EXECUTE="system services"
					;;
		-o | --system)SCRIPT_DIRS_TO_EXECUTE="system"
					;;
		-s | --services)SCRIPT_DIRS_TO_EXECUTE="services"
					;;
		-l | --log ) #shift
					 #LOG_FILE=$@
					;;
		-? | -h | --help) usage
					exit
					;;
		*) 			usage
					exit
					;;
	esac
	shift
done

# Initialize logging variable, if not already set
if [ -z $LOG_FILE ]; then
	TIMESTAMP=`date +%F-%H-%M-%S`
	LOG_FILE="$TIMESTAMP.log"
fi

# Initiate the logging sterr
exec 2> >(tee $LOG_FILE)

# Search the directories for scripts and execute them 
for DIR in $SCRIPT_DIRS_TO_EXECUTE; do
	for SCRIPT in `ls $DIR`; do
		if [[ $ANSWER == q ]]; then
			exit
		elif [[ $ANSWER != a ]]; then
			ANSWER=
		fi
		
		# Set the variables to run the check
		. ./$DIR/$SCRIPT
		
		#Log before running script, so error output is associated better
		log newline time script
			
		# Check if we should run the script or not
		# PRECHECK will return a string which explains why the solution does not need to be run
		if [ -z "$(eval $PRECHECK)"  ]; then
			log question
			runScript
			#Insert usleep because the scripts were returning too quickly and interferring 
			#with the script that follows
			usleep
			log solution answer
		else
			log precheck
		fi
	done
done